/**
 * @class   wosh::MethodsProvider
 * @brief   Collects a set of wosh::Method as sorted associative container.
 *          Based on wosh::MapT.
 * 
 *
 * @see     wosh::Method
 ****************************************************************************
 * @version 0.8.499 $Id: MethodsProvider.h 2488 2010-05-21 01:48:27Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR MethodsProviderS;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_MethodsProvider_H__
 #define __WOSH_Core_MethodsProvider_H__

 #include <woshDefs.h>
 #include <core/Containers.h>
 #include <core/Method.h>
 #include <core/Permission.h>
 #include <core/Reflection.h>


namespace wosh {

 #define _MethodsProvider_METHOD_methodlist			"methodlist"
 #define _MethodsProvider_METHOD_methodinfo			"methodinfo"
 #define _MethodsProvider_METHOD_methodNotFound		"method_not_found"
 #define _MethodsProvider_METHOD_methodInvalidArgs	"method_invalid_args"

class Message;
class Object;
class List;
class Table;

class MethodsProvider :		public MapT<std::string, Method*>,
							public virtual IReflection
 {
	WOSH_CLASSNAME(wosh::PropertiesProvider)
	WOSH_CLASS_ABSTRACT(false)


	public:
		/**
		 * @brief  Default constructor.
		 * @param owner [in] the parent object, used to initialize listPermission
		 */
		MethodsProvider( const Object* owner = NULL );
		/**
		 * @brief  Destructor, clear registered methods.
		 */
		virtual ~MethodsProvider();


/** @name Method Setters (reentrant)
 * @{
 ******************************************************************************/
 	public:
		/**
		 * @brief   Register a Method. Reentrant. 
		 * @param method [in] Method istance, ownership of the object is acquired
		 * @return  WRET_ERR_PARAM if the argument is NULL or its name already exists; WRET_OK else
		 * @note    Method::getName() is used as (hash) key
		 */
		virtual WRESULT set( Method* method );

		/**
		 * @brief   Unregister a Method. Reentrant. 
		 * @param method_name [in] name of the Method to be removed
		 * @return  WRET_ERR_PARAM if the argument is empty or Method is not found; WRET_OK else
		 * @note    the Method (object) is destroyed
		 */
		virtual WRESULT unSet( const std::string& method_name );

		/**
		 * @brief   Create a new Method of given type and register it. Reentrant. Provided for convenience. 
		 * @return  instance of the new Method
		 * @see     set()
		 */
		template <typename T>
		T* create() {
			T* method = new T();
			set(method);
			return method;
		 }

		/**
		 * @brief   Create a new Method of given type (and name) and register it. Reentrant. Provided for convenience. 
		 * @param method_name [in] name of the Method (to be passed to Method's constructor)
		 * @return  instance of the new Method
		 * @see     set()
		 */
		template <typename T>
		T* create( const std::string& method_name ) {
			T* method = new T(method_name);
			set(method);
			return method;
		 }

		/**
		 * @brief   Create a new Method of given type (and initializers) and register it. Reentrant. Provided for convenience. 
		 * @param name [in] name of the Method (to be passed to Method's constructor)
		 * @param description [in] description of the Method (to be passed to Method's constructor)
		 * @param perm [in] permission of the Method (to be passed to Method's constructor)
		 * @return  instance of the new Method
		 * @see     set()
		 */
		template <typename T>
		T* create( const std::string& name, const std::string& description, const Permission& perm = Method::defaultPermission ) {
			T* method = new T(name, description, perm);
			set(method);
			return method;
		 }

//@}
/** @name Getters
 * @{
 ******************************************************************************/
 	public:
		inline const Permission& getListPermission() const	{ return this->listPermission; }
		inline Permission& getListPermission()				{ return this->listPermission; }

		List* toList( short index ) const;
		Table* toTable( bool include_permission, bool include_arguments ) const;

//@}
/** @name Setters
 * @{
 ******************************************************************************/
 	public:
		void setPermission( const Permission& value, short overwrite_mode, bool when_empty, bool nested );

//@}
	protected:
		Permission listPermission;

	private:
		MethodsProvider( const MethodsProvider& ) : MapT<std::string, Method*>() { }
		MethodsProvider& operator=( const MethodsProvider& ) { return *this; }


}; // class def

}; // namespace wosh

#endif //__WOSH_Core_MethodsProvider_H__
